// pages/menu/region/locationDevice/locationDevice.js
var app = getApp()
var verify = require('../../../../utils/verify-token.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    size: 10,
    loading: true,
    filterValue: "",
    isFresh: false,
    locationid:"123",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("deviceListoptions", this.data.locationid)
    var size = this.data.size
    var filterValue = this.data.filterValue
    var locationid = options.locationid
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var that = this

    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
      locationid: locationid
    })

    wx.request({
      url: baseUrl+'location/'+locationid+'/devices/now?page=0&size='+size,
      header: {
        'AUTHORIZATION': token
      },
      success(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          console.log("deviceList", res.data.data.content)
          var deviceList = res.data.data.content
          var totalElements = res.data.data.totalElements
          var loading = false
          if (totalElements > 10) {
            loading = true
          }
          that.setData({
            deviceList: deviceList,
            totalElements: totalElements,
            loading: loading,
            isFresh: false
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
        
      }
    })
  },

  // 添加点位设备
  addDevice:function(){
    var locationid = this.data.locationid
    wx.navigateTo({
      url: 'addDevice/addDevice?locationid=' + locationid,
    })
  },

// 编辑点位设备
  editDevice:function(res){
    console.log("edit",res)
    var locationid = this.data.locationid
    var deviceid = res.currentTarget.dataset.deviceid
    var customname = res.currentTarget.dataset.customname
    var qrcodeurl = res.currentTarget.dataset.qrcodeurl
    var telephone = res.currentTarget.dataset.telephone
    wx.navigateTo({
      url: 'editDevice/editDevice?deviceid=' + deviceid + 
            '&locationid=' + locationid+
            '&customname=' + customname+
            '&qrcodeurl=' + qrcodeurl+
            '&telephone=' + telephone,
    })
  },

  removeDevice:function(res){
    var locationid = this.data.locationid
    var deviceid = res.currentTarget.dataset.deviceid
    var customname = res.currentTarget.dataset.customname
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定将该设备移出点位？\n设备编号：' + customname,
      success(res){
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: baseUrl + 'location/' + locationid + '/device/' + deviceid,
            method: "delete",
            header: {
              'AUTHORIZATION': token
            },
            success: function (res) {
              console.log("success", res)
              if (res.data.code == 60006) {
                verify.clearAll()
              } else if (res.data.code == 200) {
                wx.showToast({
                  title: '移出成功!',
                  duration: 3500
                })
                that.reLoad()
              } else {
                wx.showModal({
                  title: '移出失败',
                  content: res.data.message,
                })
              }
            }
          })
        }
        else if (res.cancel) {
            console.log('用户点击取消')
          }
      }
    })
  },

// 重新加载函数
  reLoad:function(){
    var locationid = this.data.locationid
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var size = this.data.size
    var that = this

    wx.request({
      url: baseUrl + 'location/' + locationid + '/devices/now?page=0&size=' + size,
      header: {
        'AUTHORIZATION': token
      },
      success(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          console.log("deviceList", res.data.data.content)
          var deviceList = res.data.data.content
          var totalElements = res.data.data.totalElements
          var loading = false
          if (totalElements > 10) {
            loading = true
          }
          that.setData({
            deviceList: deviceList,
            totalElements: totalElements,
            loading: loading,
            isFresh: false
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.reLoad()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var size = this.data.size * 1 + 10
    var loading = this.data.loading
    var totalElements = this.data.totalElements
    this.setData({
      isFresh: true
    })
    if (size - 10 > totalElements) {
      this.setData({
        loading: false
      })
    } else {
      this.setData({
        size: size
      })
      this.reLoad()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})